# -*- coding: utf-8 -*-
'''
Created on Aug 22, 2012

@author: LONG HOANG GIANG
'''
import os, sys
import traceback
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import pyLib
from pyLib import html2text
import datetime
import re, json


FILE = '/longhoanggiang/activemobile/toc.html'
if not os.path.exists(os.path.dirname(FILE)):
    os.makedirs(os.path.dirname(FILE), 0777)
    
    
def readFile(filePath):
    f = open(filePath, 'r')
    data = f.read()
    f.close()
    return data

def getDetail(fileName):
    print fileName
    if re.search(r"(\d)+html", fileName): 
        fileName = re.sub(r"(\d+)html", "\\1.html", fileName)
    filePath = os.path.join(os.path.dirname(FILE), fileName)
    html = readFile(filePath)
    html = html2text.html2text(html, w100=True, prefix="files/").strip()
    html = re.sub(ur"<b>\n\s*\n", "<b>", html)
    html = re.sub(ur"\n\s*\n\s*</b>", "</b>", html)
    html = re.sub(ur"(\s*\n\s*){3,}", "\n\n", html)
    html = re.sub(ur"\n", "<br />", html)
    html = re.sub(ur"<br /><br /><b></b><br /><br />", "<br /><hr /><br />", html)
    html = re.sub(ur" {3,}", " ", html)
    return html

def process():
    html = readFile(FILE)
    tree = pyLib.buildTree(html)
    
    output_folder = "/longhoanggiang/truyentext/{0}/".format(DATANAME)
    file_output_folder = output_folder + 'files' + '/'
    if not os.path.isdir(file_output_folder):
        os.makedirs(file_output_folder, 0777)
    
    data = []
    for item in tree.xpath("//a"):
        try:
            filename = item.get('href')
            if filename == None: continue
            name = pyLib.toUpper(pyLib.normalizeStr(pyLib.stringify(item)))
            content = getDetail(filename)
            print name, content
            fileId = pyLib.crc32unsigned("{0}{1}".format(name,filename))
            pyLib.gzip(file_output_folder + fileId, json.dumps(content))
            data.append({'name': name, 'path': fileId})
        except:
            traceback.print_exc()
    pyLib.gzip(output_folder + 'data', pyLib.encryptCipher(json.dumps(data)))
    
    

if __name__ == '__main__':
    
    DATANAME = raw_input("Enter ouput folder name: ")
    if DATANAME.strip() != '':
        process()
    else:
        print 'You must enter folder name not empty'
    print '>Finished at {0}'.format(datetime.datetime.now())
    os._exit(1)
    